package fr.uvsq.isty.bdh.hchess.util;

import fr.uvsq.isty.bdh.hchess.util.ConfigFile;
import java.io.File;
import java.io.IOException;

/**
 * Application global configuration.
 * Allow any application classes to store in the data directory
 * and in the config file.
 * 
 * @version 1.0
 * @author Florent Hemmi <florent.hemmi@gmail.com>
 */
public class GlobalConfig {
    
    /** Unique instance */
    private static GlobalConfig instance = null;
    
    /** Folder when data is stored */
    public static final String DIR = "data/";
    
    /** INI filename */
    private String iniFilename;
    private ConfigFile ini;
    
    /** Private constructor for the initialization */
    private GlobalConfig(String iniFilename) throws IOException {
        this.iniFilename = iniFilename;
        File path = new File(DIR);
        path.mkdir();
        path = new File(DIR+iniFilename);
        path.createNewFile();
        
        this.ini = new ConfigFile(path);
    }
    
    /**
     * Call this method to retrieve the instance of the object.
     * In fact, constructor cannot be called directly.
     * 
     * @return An instance of the object (the same as before if already created)
     */
    public static ConfigFile getInstance() throws IOException {
        if (instance == null) {
            instance = new GlobalConfig("prefs.ini");
        }
        return instance.ini;
    }
}
